const { app, BrowserWindow } = require("electron/main");
const { handleWindow } = require("./lib/handleWindowInstance");
const { handleRegisterIpcMain, handleUnregisterIpcMain } = require("./lib/handleIpc");
const { handleGlobalShortcut, handleUnregisterAll } = require("./lib/handleGlobalShortcut");
const { handleAppUpdate } = require("./lib/handleAppUpdate");
const { handleExecuteCopy, handleClearCopy } = require("./lib/handleClipboard");
const { handleTraceMainPerformance } = require("./lib/handleTracePerformance");
const { handleCrashReporter } = require("./lib/handleCrashReporter");
const { handleDesktopCapturer } = require("./lib/handleDesktopCapturer");
const { handleShowOpenDialog, handleShowSaveDialog } = require('./lib/handleDialog')
const { handleNotification } = require('./lib/handleNotification')
const { getScreenInfo } = require('./lib/handleScreen')
const { openExternal } = require('./lib/handleShell')
const { handleTray } = require('./lib/handleTray')
const { handleAppProtocolLaunch } = require('./lib/handleAppLaunch')

const handleAppFunction = () => {
	// 当前应用程序目录
	console.log(app.getAppPath())

	// 根据名称获取路径(home、appData、userData)
	console.log(app.getAppPath('appData'))

	// 获取应用程序的版本号
	console.log(app.getVersion())

	// 获取应用程序的名称
	console.log(app.getName())

	// 设置应用程序名称
	app.setName('设置应用名称')
}

const handleAppEvent = () => {
	// 判断当前应用是否抢到运行锁(解释: 防止应用打开第二次, 第二次打开的应用则退出, 第一次打开的应用则聚焦回显)
	const checkWinLock = app.requestSingleInstanceLock();
	if (!checkWinLock) {
		app.quit();
	} else {
		app.on('second-instance', (event, commandLine, workingDirectory) => {
			console.log('应用再次被打开 => ', commandLine, workingDirectory)
			const mainWindow = BrowserWindow.getAllWindows()[0];
			if (mainWindow) {
				if (!mainWindow.isMinimized()) {
					mainWindow.restore();
				}
				mainWindow.focus();
			}
		})

		app.on('will-finish-launching', () => {
			console.log('electron完成基础的启动触发')
		})

		app.on("ready", () => {
			console.log('electron完成初始化触发')
		})

		app.whenReady().then(() => {
			const windowList = handleWindow()

			// 应用程序被激活时触发
			app.on("activate", () => {
				if (BrowserWindow.getAllWindows().length === 0) {
					handleWindow()
				}
			});
			
			// if (false) {
			// 	handleShowOpenDialog().then(res => {
			// 		console.log('打开文件返回数据', res)
			// 	});
			// }
			handleCrashReporter()
			handleAppProtocolLaunch();
			handleTray();
			handleNotification();
			handleDesktopCapturer();
			handleAppFunction();
			handleGlobalShortcut();
			handleAppUpdate();
			handleExecuteCopy('test content');
			handleRegisterIpcMain(windowList);
			handleTraceMainPerformance();
		});
		
		app.on("before-quit", () => {
			console.log('electron关闭窗口前触发')
			handleUnregisterIpcMain()
		})

		app.on("window-all-closed", () => {
			console.log('electron所有窗口都被关闭时触发')
			if (process.platform !== "darwin") {
				app.quit();
			}
		})

		app.on("will-quit", () => {
			console.log('electron所有窗口关闭后触发')
		})

		app.on("quit", () => {
			console.log('electron应用程序退出时发出')
		})

		app.on("render-process-gone", (event, webContents, details) => {
			console.log('渲染进程进程崩溃或被杀死')
			console.log(webContents, details)
		})
	}
}

handleAppEvent();


